package cn.kioye.system;

import java.util.List;

import cn.kioye.system.ResultAvailableTimeDTO.*;
import cn.kioye.system.ResultAvailablePointDTO.*;
import org.springframework.http.HttpHeaders;

/**
 * @date 2021-06-02 下午 12:45
 */
public class Main {

    public static void replaceHeader(HttpHeaders headers, String key, String value) {
        headers.remove(key);
        headers.add(key, value);
    }


    public static void main(String[] args) {
        String visitcode = "Pi0ynNDpmTJMf66i";
        String token = "51f78cf709654805da3aa0565271f4f1";
        // 地区代号
        String areaId = "440305";
        // 疫苗代号
        String vaccCode = "5601";
        // 厂商
        String corpCode = "80";
        // 不知道啥ID
        Long pid = 4651125L;

        // *****个人信息****
        // 个人档案ID
        String reusId = "593440952879934564";
        // 姓名
        String reusTrueName = "张三";
        // 身份证ID
        String reusIdCard = "360734199905303411";
        // 出生日期
        String reusBirthday = "1999-05-30";
        // 联系电话
        String reusMobile = "138563245446";


        // 请求头参数
        HttpHeaders requestHeaders = new HttpHeaders();
        replaceHeader(requestHeaders, "Host", "szhc.newhealth.com.cn");
        replaceHeader(requestHeaders, "Accept-Encoding", "gzip, deflate, br");
        replaceHeader(requestHeaders, "Connection", "keep-alive");
        replaceHeader(requestHeaders, "Accept", "*/*");
        replaceHeader(requestHeaders, "User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000239) NetType/WIFI Language/en");
        replaceHeader(requestHeaders, "Referer", "https://servicewechat.com/wx3e5867e517b2122f/77/page-frame.html");
        replaceHeader(requestHeaders, "Accept-Language", "en-us");
        replaceHeader(requestHeaders, "Content-Type", "application/json");

        // 开始执行
        AppointVaccineService appointVaccineService = new AppointVaccineService();
        while (true) {
            List<ResultAvailablePoint> availablePoint = appointVaccineService.getAvailablePoint(requestHeaders, visitcode, token,pid,
                    vaccCode, corpCode, areaId);
            if (availablePoint == null || availablePoint.isEmpty()) {
                continue;
            }
            for (ResultAvailablePoint resultAvailablePoint : availablePoint) {
                if (resultAvailablePoint.getNums() > 0) {
                    System.out.println(String.format("发现疫苗点：%s 数量：%d 地址：%s", resultAvailablePoint.getOutpName(), resultAvailablePoint.getNums(), resultAvailablePoint.getOutpAddress()));
                    // order check time
                    List<ResultAvailableTimePage> availableTimes = appointVaccineService.getAvailableTime(requestHeaders, visitcode, token,pid, resultAvailablePoint.getDepaId());
                    if (availableTimes == null || availableTimes.isEmpty()) {
                        continue;
                    }
                    for (ResultAvailableTimePage resultAvailableTimePage : availableTimes) {
                        if (resultAvailableTimePage.getAfternoon() == null) {
                            continue;
                        }
                        List<ResultAvailableTime> resultAvailableTimes = resultAvailableTimePage.getAfternoon();
                        if (resultAvailableTimes == null && resultAvailableTimes.isEmpty()) {
                            continue;
                        }
                        for (ResultAvailableTime resultAvailableTime : resultAvailableTimes) {
                            if (resultAvailableTime.getAvail() != null && resultAvailableTime.getAvail() > 0) {
                                if (resultAvailableTime.getSource().isEmpty()) {
                                    continue;
                                }
                                for (ResultAvailableTimeSource resultAvailableTimeSource : resultAvailableTime.getSource()) {
                                    if (resultAvailableTimeSource.getAvail() != null && resultAvailableTimeSource.getAvail() > 0) {
                                        // 发现可用疫苗
                                        System.out.println(String.format("发现可用疫苗：%s 数量：%d 时间：%s %s -> %s 地址：%s ", resultAvailablePoint.getOutpName(),
                                                resultAvailablePoint.getNums(), resultAvailableTime.getSdate(),
                                                resultAvailableTimeSource.getTimeBegin(), resultAvailableTimeSource.getTimeEnd(), resultAvailablePoint.getOutpAddress()));
                                        // 直接定
                                        String s = appointVaccineService.tryOrder(requestHeaders, visitcode, token,pid, resultAvailablePoint.getDepaId(), resultAvailablePoint.getOutpName(),
                                                resultAvailablePoint.getAreaName(), resultAvailablePoint.getCorpCode(), resultAvailableTime.getSdate(),
                                                resultAvailableTimeSource.getTimeBegin(), resultAvailableTimeSource.getTimeEnd(), resultAvailableTimeSource.getOuatId(),
                                                vaccCode,reusId,reusTrueName,reusIdCard,reusBirthday,reusMobile);
                                        System.out.println("预定结果：" + s);
                                        return;
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
    }
}
